package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.GwaService;
import cn.e4j.modular.api.service.Gwat153Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwaklT;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;


/**
 * Created by yuhuan on 2018/07/10.
 */
@RestController
@RequestMapping("api/wx/gwat153")
public class Gwat153Controller {

    private static Logger logger = LoggerFactory.getLogger(Gwat153Controller.class);

    @Autowired
    private Gwat153Service gwat153Service;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    GwbService gwbService;

    @Autowired
    GwaService gwaService;

    /**
     * 获取商机主题列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getGwbgPage")
    public ResponseData getGwbgPage(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWAK019", currentUser.getACCOUNT());

            //客户权限
            Dto gwb001 = gwbService.getGwb001(currentUser, inDto);
            inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(currentUser, (List<Dto>) sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }

        return new SuccessResponseData(gwat153Service.getGwbgPage(inDto));
    }


    /**
     * 获取客户信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getGwakPage")
    public ResponseData getGwakPage(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        inDto.put("GWAK019", currentUser.getACCOUNT());
        List<Dto> list = gwat153Service.getGwakPage(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 获取经销商信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getJxsPage")
    public ResponseData getJxsPage(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        //UserModel currentUser = UserUtils.getCurrentCacheUser();
        //inDto.put("gwak019", currentUser.getAccount_());

        //客户权限
        inDto.put("GWAK006", "2");
        Dto gwb001 = gwbService.getGwb001(user, inDto);
        inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

        //上下级权限
        Dto sys003 = systemService.getSys003(user);
        inDto.put("ORGS", sys003.get("ORGS"));

        List<Dto> list = gwat153Service.getJxsPage(wapper.getPage(),inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 保存经销商信息 remove 换成 gwat200/insertGwak
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveJxs")
    public ResponseData saveJxs(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();

        GwakT gwak_tPO = new GwakT();
        GwaklT gwakl_tPO = new GwaklT();
        GwalT gwal_tPO = new GwalT();
        try {
            gwak_tPO = JSON.parseObject(JSON.toJSONString(inDto),GwakT.class);
            gwak_tPO.setGWAKUSER(currentUser.getID());
            gwak_tPO.setGWAKMODU(currentUser.getID());
            gwak_tPO.setGWAKORIU(currentUser.getID());
            gwak_tPO.setGWAKGRUP(currentUser.getORG_ID());
            gwak_tPO.setGWAKORIG(currentUser.getORG_ID());
            gwak_tPO.setGWAKDATE( LocalDate.now());
            gwak_tPO.setGWAKTIME( DateUtils.getTime());
//            gwak_tPO._insert();
            gwak_tPO.setGWAKENT(currentUser.getDBC());
            gwak_tPO.setGWAK002("1"); // 1.客户、2.厂商、3.两者
            gwak_tPO.setGWAK006("2"); //1.最终客户、2.经销商、3.内部法人、4.其他类
            gwak_tPO.setGWAK007("*");
            gwak_tPO.setGWAK008("CNY");
            gwak_tPO.setGWAK009("*"); //客户分类
            gwak_tPO.setGWAK010("*"); //客户渠道
            gwak_tPO.setGWAK019(currentUser.getACCOUNT());
            gwak_tPO.setGWAK020("ASIA");
            gwak_tPO.setGWAK021("CN");
            gwak_tPO.setGWAKACTI("Y");
            gwak_tPO.setGWAKDATE(LocalDate.now());
            gwak_tPO.setGWAKORIU(currentUser.getID());

            // add by wenchuan.li 2018/10/26 13:31
            Dto sys002Dto = new Dto();
            sys002Dto.put("ORG_ID", currentUser.getORG_ID());
            Dto sys002 = systemService.getSys002(sys002Dto);
            gwak_tPO.setGWAKPLANT(sys002.getString("ID"));//法人
            gwak_tPO.setGWAK056(BigDecimal.ONE);//折扣率

            gwakl_tPO = JSON.parseObject(JSON.toJSONString(inDto),GwaklT.class);
            gwakl_tPO.setGWAKLENT(currentUser.getDBC());
            gwakl_tPO.setGWAKL004(gwakl_tPO.getGWAKL003());
            gwakl_tPO.setGWAKL002("2");

            Dto inDto1 = new Dto();
            inDto1.put("GWAKENT", inDto.getString("ENT"));
            Dto dto = gwat153Service.getGwak001(inDto1);
            if (dto.containsKey("GWAK001") && ToolUtil.isNotEmpty(dto.getString("GWAK001"))) {
                String gwak001 = dto.getString("GWAK001");
                int no = Integer.parseInt(gwak001.replace("C", "")) + 1;
                String gwak001New = "C" + String.valueOf(no);
                gwak_tPO.setGWAK001(gwak001New);
                gwakl_tPO.setGWAKL001(gwak001New);
                gwakl_tPO.setGWAKL005(gwak001New);
            } else {
                gwak_tPO.setGWAK001("C1000");
                gwakl_tPO.setGWAKL001("C1000");
                gwakl_tPO.setGWAKL005("C1000");
            }

            gwal_tPO.setGWALUSER(currentUser.getID());
            gwal_tPO.setGWALMODU(currentUser.getID());
            gwal_tPO.setGWALORIU(currentUser.getID());
            gwal_tPO.setGWALGRUP(currentUser.getORG_ID());
            gwal_tPO.setGWALORIG(currentUser.getORG_ID());
            gwal_tPO.setGWALDATE( LocalDate.now());
            gwal_tPO.setGWALTIME( DateUtils.getTime());
//            gwal_tPO._insert();
            gwal_tPO.setGWALENT(currentUser.getDBC());
            gwal_tPO.setGWAL001(gwak_tPO.getGWAK001());

            Dto d = new Dto();
            d.put("GWAL001",gwak_tPO.getGWAK001());
            d.put("GWALENT", inDto.getString("ENT"));
            gwal_tPO.setGWAL002(gwaService.getGwa002(d));

            gwal_tPO.setGWAL003("Y");
            gwal_tPO.setGWAL010("ASIA");
            gwal_tPO.setGWAL011("CN");
            gwal_tPO.setGWAL012(gwak_tPO.getGWAK022());
            gwal_tPO.setGWAL013(gwak_tPO.getGWAK023());
            gwal_tPO.setGWAL014(gwak_tPO.getGWAK024());
            gwal_tPO.setGWAL015(gwak_tPO.getGWAK025());
            gwal_tPO.setGWAL016(ToolUtil.isNotEmpty(gwak_tPO.getGWAK026()) ? gwak_tPO.getGWAK026() : "*");
            gwal_tPO.setGWAL017(ToolUtil.isNotEmpty(gwak_tPO.getGWAK027()) ? gwak_tPO.getGWAK027() : "*");
            gwal_tPO.setGWAL018(ToolUtil.isNotEmpty(gwak_tPO.getGWAK028()) ? gwak_tPO.getGWAK028(): "*");
            gwal_tPO.setGWAL019(ToolUtil.isNotEmpty(gwak_tPO.getGWAK029()) ? gwak_tPO.getGWAK029(): "*");
            gwal_tPO.setGWAL020(ToolUtil.isNotEmpty(gwak_tPO.getGWAK030())? gwak_tPO.getGWAK030() : "*");
            gwal_tPO.setGWAL021(gwak_tPO.getGWAK031());
            gwal_tPO.setGWALACTI("Y");

            gwat153Service.saveJxs(gwak_tPO, gwakl_tPO, gwal_tPO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器开小差了");
        }

        return new SuccessResponseData();
    }


    /**
     * 保存或修改商机主题
     *
     * @return
     */
    @RequestMapping(value = "gwat153Save")
    @ResponseBody
    public ResponseData orderSave(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            gwat153Service.gwat153Save(wapper.getSysUser(),inDto);

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }
        return new SuccessResponseData();
    }


    /**
     * 删除商机主题
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteTheme")
    public ResponseData deleteTheme(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("GWBHACTI", "Y");
            gwat153Service.deleteTheme(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }

    /**
     * 检查商机主题是否存在拜访记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkTheme")
    public ResponseData checkTheme(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = new Dto();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("GWBHACTI", "Y");
            outDto.put("count", gwat153Service.checkTheme(inDto));
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("系统出小差了");
        }
        return new SuccessResponseData(outDto);
    }

    /**
     * 检查商机主题是否存在
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkExist")
    public ResponseData checkExist(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = new Dto();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("GWBHACTI", "Y");
            outDto.put("count", gwat153Service.checkExist(inDto));
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(outDto);
    }

    /**
     * 获取该商机主题对应的经销商列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "jxsList")
    public ResponseData jxsList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat153Service.jxsList(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 获取该商机主题对应的经销商列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "listUser")
    public ResponseData listUser(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = null;
        try {
            inDto.put("ENT", inDto.getString("ENT"));
            list = gwat153Service.listUserPage(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(list);
    }

}
